<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>prompt.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_prompt.js.html">prompt.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview DHTML prompt to replace javascript&#39;s prompt().
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/prompt.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.Prompt&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.functions&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.Component.Error&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Dialog&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Dialog.ButtonSet&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.Dialog.DefaultButtonKeys&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Dialog.EventType&#39;);
<a name="line34"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Creates an object that represents a prompt (used in place of javascript&#39;s
<a name="line40"></a> * prompt). The html structure of the prompt is the same as the layout for
<a name="line41"></a> * dialog.js except for the addition of a text box which is placed inside the
<a name="line42"></a> * &quot;Content area&quot; and has the default class-name &#39;modal-dialog-userInput&#39;
<a name="line43"></a> *
<a name="line44"></a> * @param {string} promptTitle The title of the prompt.
<a name="line45"></a> * @param {string} promptText The text of the prompt.
<a name="line46"></a> * @param {Function} callback The function to call when the user selects Ok or
<a name="line47"></a> *     Cancel. The function should expect a single argument which represents
<a name="line48"></a> *     what the user entered into the prompt. If the user presses cancel, the
<a name="line49"></a> *     value of the argument will be null.
<a name="line50"></a> * @param {string=} opt_defaultValue Optional default value that should be in
<a name="line51"></a> *     the text box when the prompt appears.
<a name="line52"></a> * @param {string=} opt_class Optional prefix for the classes.
<a name="line53"></a> * @param {boolean=} opt_useIframeForIE For IE, workaround windowed controls
<a name="line54"></a> *     z-index issue by using a an iframe instead of a div for bg element.
<a name="line55"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link
<a name="line56"></a> *    goog.ui.Component} for semantics.
<a name="line57"></a> * @constructor
<a name="line58"></a> * @extends {goog.ui.Dialog}
<a name="line59"></a> */
<a name="line60"></a>goog.ui.Prompt = function(promptTitle, promptText, callback, opt_defaultValue,
<a name="line61"></a>    opt_class, opt_useIframeForIE, opt_domHelper) {
<a name="line62"></a>  goog.base(this, opt_class, opt_useIframeForIE, opt_domHelper);
<a name="line63"></a>
<a name="line64"></a>  /**
<a name="line65"></a>   * The id of the input element.
<a name="line66"></a>   * @type {string}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.inputElementId_ = this.makeId(&#39;ie&#39;);
<a name="line70"></a>
<a name="line71"></a>  this.setTitle(promptTitle);
<a name="line72"></a>  this.setContent(&#39;&lt;label for=&quot;&#39; + this.inputElementId_ + &#39;&quot;&gt;&#39; + promptText +
<a name="line73"></a>      &#39;&lt;/label&gt;&lt;br&gt;&lt;br&gt;&#39;);
<a name="line74"></a>  this.callback_ = callback;
<a name="line75"></a>  this.defaultValue_ = goog.isDef(opt_defaultValue) ? opt_defaultValue : &#39;&#39;;
<a name="line76"></a>
<a name="line77"></a>  /** @desc label for a dialog button. */
<a name="line78"></a>  var MSG_PROMPT_OK = goog.getMsg(&#39;OK&#39;);
<a name="line79"></a>  /** @desc label for a dialog button. */
<a name="line80"></a>  var MSG_PROMPT_CANCEL = goog.getMsg(&#39;Cancel&#39;);
<a name="line81"></a>  var buttonSet = new goog.ui.Dialog.ButtonSet(opt_domHelper);
<a name="line82"></a>  buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.OK, MSG_PROMPT_OK, true);
<a name="line83"></a>  buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.CANCEL,
<a name="line84"></a>      MSG_PROMPT_CANCEL, false, true);
<a name="line85"></a>  this.setButtonSet(buttonSet);
<a name="line86"></a>};
<a name="line87"></a>goog.inherits(goog.ui.Prompt, goog.ui.Dialog);
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Callback function which is invoked with the response to the prompt
<a name="line92"></a> * @type {Function}
<a name="line93"></a> * @private
<a name="line94"></a> */
<a name="line95"></a>goog.ui.Prompt.prototype.callback_ = goog.nullFunction;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Default value to display in prompt window
<a name="line100"></a> * @type {string}
<a name="line101"></a> * @private
<a name="line102"></a> */
<a name="line103"></a>goog.ui.Prompt.prototype.defaultValue_ = &#39;&#39;;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Element in which user enters response (HTML &lt;input&gt; text box)
<a name="line108"></a> * @type {HTMLInputElement}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.ui.Prompt.prototype.userInputEl_ = null;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Tracks whether the prompt is in the process of closing to prevent multiple
<a name="line116"></a> * calls to the callback when the user presses enter.
<a name="line117"></a> * @type {boolean}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.ui.Prompt.prototype.isClosing_ = false;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Number of rows in the user input element.
<a name="line125"></a> * The default is 1 which means use an &lt;input&gt; element.
<a name="line126"></a> * @type {number}
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.ui.Prompt.prototype.rows_ = 1;
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Number of cols in the user input element.
<a name="line134"></a> * The default is 0 which means use browser default.
<a name="line135"></a> * @type {number}
<a name="line136"></a> * @private
<a name="line137"></a> */
<a name="line138"></a>goog.ui.Prompt.prototype.cols_ = 0;
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * The input decorator function.
<a name="line143"></a> * @type {function(Element)?}
<a name="line144"></a> * @private
<a name="line145"></a> */
<a name="line146"></a>goog.ui.Prompt.prototype.inputDecoratorFn_ = null;
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * A validation function that takes a string and returns true if the string is
<a name="line151"></a> * accepted, false otherwise.
<a name="line152"></a> * @type {function(string):boolean}
<a name="line153"></a> * @private
<a name="line154"></a> */
<a name="line155"></a>goog.ui.Prompt.prototype.validationFn_ = goog.functions.TRUE;
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Sets the validation function that takes a string and returns true if the
<a name="line160"></a> * string is accepted, false otherwise.
<a name="line161"></a> * @param {function(string): boolean} fn The validation function to use on user
<a name="line162"></a> *     input.
<a name="line163"></a> */
<a name="line164"></a>goog.ui.Prompt.prototype.setValidationFunction = function(fn) {
<a name="line165"></a>  this.validationFn_ = fn;
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/** @override */
<a name="line170"></a>goog.ui.Prompt.prototype.enterDocument = function() {
<a name="line171"></a>  if (this.inputDecoratorFn_) {
<a name="line172"></a>    this.inputDecoratorFn_(this.userInputEl_);
<a name="line173"></a>  }
<a name="line174"></a>  goog.ui.Prompt.superClass_.enterDocument.call(this);
<a name="line175"></a>  this.getHandler().listen(this,
<a name="line176"></a>      goog.ui.Dialog.EventType.SELECT, this.onPromptExit_);
<a name="line177"></a>
<a name="line178"></a>  this.getHandler().listen(this.userInputEl_,
<a name="line179"></a>      [goog.events.EventType.KEYUP, goog.events.EventType.CHANGE],
<a name="line180"></a>      this.handleInputChanged_);
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * @return {HTMLInputElement} The user input element. May be null if the Prompt
<a name="line186"></a> *     has not been rendered.
<a name="line187"></a> */
<a name="line188"></a>goog.ui.Prompt.prototype.getInputElement = function() {
<a name="line189"></a>  return this.userInputEl_;
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Sets an input decorator function.  This function will be called in
<a name="line195"></a> * #enterDocument and will be passed the input element.  This is useful for
<a name="line196"></a> * attaching handlers to the input element for specific change events,
<a name="line197"></a> * for example.
<a name="line198"></a> * @param {function(Element)} inputDecoratorFn A function to call on the input
<a name="line199"></a> *     element on #enterDocument.
<a name="line200"></a> */
<a name="line201"></a>goog.ui.Prompt.prototype.setInputDecoratorFn = function(inputDecoratorFn) {
<a name="line202"></a>  this.inputDecoratorFn_ = inputDecoratorFn;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Set the number of rows in the user input element.
<a name="line208"></a> * A values of 1 means use an &lt;input&gt; element.  If the prompt is already
<a name="line209"></a> * rendered then you cannot change from &lt;input&gt; to &lt;textarea&gt; or vice versa.
<a name="line210"></a> * @param {number} rows Number of rows for user input element.
<a name="line211"></a> * @throws {goog.ui.Component.Error.ALREADY_RENDERED} If the component is
<a name="line212"></a> *    already rendered and an attempt to change between &lt;input&gt; and &lt;textarea&gt;
<a name="line213"></a> *    is made.
<a name="line214"></a> */
<a name="line215"></a>goog.ui.Prompt.prototype.setRows = function(rows) {
<a name="line216"></a>  if (this.isInDocument()) {
<a name="line217"></a>    if (this.userInputEl_.tagName.toLowerCase() == &#39;input&#39;) {
<a name="line218"></a>      if (rows &gt; 1) {
<a name="line219"></a>        throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line220"></a>      }
<a name="line221"></a>    } else {
<a name="line222"></a>      if (rows &lt;= 1) {
<a name="line223"></a>        throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line224"></a>      }
<a name="line225"></a>      this.userInputEl_.rows = rows;
<a name="line226"></a>    }
<a name="line227"></a>  }
<a name="line228"></a>  this.rows_ = rows;
<a name="line229"></a>};
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * @return {number} The number of rows in the user input element.
<a name="line234"></a> */
<a name="line235"></a>goog.ui.Prompt.prototype.getRows = function() {
<a name="line236"></a>  return this.rows_;
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Set the number of cols in the user input element.
<a name="line242"></a> * @param {number} cols Number of cols for user input element.
<a name="line243"></a> */
<a name="line244"></a>goog.ui.Prompt.prototype.setCols = function(cols) {
<a name="line245"></a>  this.cols_ = cols;
<a name="line246"></a>  if (this.userInputEl_) {
<a name="line247"></a>    if (this.userInputEl_.tagName.toLowerCase() == &#39;input&#39;) {
<a name="line248"></a>      this.userInputEl_.size = cols;
<a name="line249"></a>    } else {
<a name="line250"></a>      this.userInputEl_.cols = cols;
<a name="line251"></a>    }
<a name="line252"></a>  }
<a name="line253"></a>};
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * @return {number} The number of cols in the user input element.
<a name="line258"></a> */
<a name="line259"></a>goog.ui.Prompt.prototype.getCols = function() {
<a name="line260"></a>  return this.cols_;
<a name="line261"></a>};
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * Create the initial DOM representation for the prompt.
<a name="line266"></a> * @override
<a name="line267"></a> */
<a name="line268"></a>goog.ui.Prompt.prototype.createDom = function() {
<a name="line269"></a>  goog.ui.Prompt.superClass_.createDom.call(this);
<a name="line270"></a>
<a name="line271"></a>  var cls = this.getClass();
<a name="line272"></a>
<a name="line273"></a>  // add input box to the content
<a name="line274"></a>  var attrs = {
<a name="line275"></a>    &#39;className&#39;: goog.getCssName(cls, &#39;userInput&#39;),
<a name="line276"></a>    &#39;value&#39;: this.defaultValue_};
<a name="line277"></a>  if (this.rows_ == 1) {
<a name="line278"></a>    // If rows == 1 then use an input element.
<a name="line279"></a>    this.userInputEl_ = /** @type {HTMLInputElement} */
<a name="line280"></a>        (this.getDomHelper().createDom(&#39;input&#39;, attrs));
<a name="line281"></a>    this.userInputEl_.type = &#39;text&#39;;
<a name="line282"></a>    if (this.cols_) {
<a name="line283"></a>      this.userInputEl_.size = this.cols_;
<a name="line284"></a>    }
<a name="line285"></a>  } else {
<a name="line286"></a>    // If rows &gt; 1 then use a textarea.
<a name="line287"></a>    this.userInputEl_ = /** @type {HTMLInputElement} */
<a name="line288"></a>        (this.getDomHelper().createDom(&#39;textarea&#39;, attrs));
<a name="line289"></a>    this.userInputEl_.rows = this.rows_;
<a name="line290"></a>    if (this.cols_) {
<a name="line291"></a>      this.userInputEl_.cols = this.cols_;
<a name="line292"></a>    }
<a name="line293"></a>  }
<a name="line294"></a>
<a name="line295"></a>  this.userInputEl_.id = this.inputElementId_;
<a name="line296"></a>  var contentEl = this.getContentElement();
<a name="line297"></a>  contentEl.appendChild(this.getDomHelper().createDom(
<a name="line298"></a>      &#39;div&#39;, {&#39;style&#39;: &#39;overflow: auto&#39;}, this.userInputEl_));
<a name="line299"></a>
<a name="line300"></a>  if (this.rows_ &gt; 1) {
<a name="line301"></a>    // Set default button to null so &lt;enter&gt; will work properly in the textarea
<a name="line302"></a>    this.getButtonSet().setDefault(null);
<a name="line303"></a>  }
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * Handles input change events on the input field.  Disables the OK button if
<a name="line309"></a> * validation fails on the new input value.
<a name="line310"></a> * @private
<a name="line311"></a> */
<a name="line312"></a>goog.ui.Prompt.prototype.handleInputChanged_ = function() {
<a name="line313"></a>  this.updateOkButtonState_();
<a name="line314"></a>};
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/**
<a name="line318"></a> * Set OK button enabled/disabled state based on input.
<a name="line319"></a> * @private
<a name="line320"></a> */
<a name="line321"></a>goog.ui.Prompt.prototype.updateOkButtonState_ = function() {
<a name="line322"></a>  var enableOkButton = this.validationFn_(this.userInputEl_.value);
<a name="line323"></a>  var buttonSet = this.getButtonSet();
<a name="line324"></a>  buttonSet.setButtonEnabled(goog.ui.Dialog.DefaultButtonKeys.OK,
<a name="line325"></a>      enableOkButton);
<a name="line326"></a>};
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>/**
<a name="line330"></a> * Causes the prompt to appear, centered on the screen, gives focus
<a name="line331"></a> * to the text box, and selects the text
<a name="line332"></a> * @param {boolean} visible Whether the dialog should be visible.
<a name="line333"></a> * @override
<a name="line334"></a> */
<a name="line335"></a>goog.ui.Prompt.prototype.setVisible = function(visible) {
<a name="line336"></a>  goog.base(this, &#39;setVisible&#39;, visible);
<a name="line337"></a>
<a name="line338"></a>  if (visible) {
<a name="line339"></a>    this.isClosing_ = false;
<a name="line340"></a>    this.userInputEl_.value = this.defaultValue_;
<a name="line341"></a>    this.focus();
<a name="line342"></a>    this.updateOkButtonState_();
<a name="line343"></a>  }
<a name="line344"></a>};
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * Overrides setFocus to put focus on the input element.
<a name="line349"></a> * @override
<a name="line350"></a> */
<a name="line351"></a>goog.ui.Prompt.prototype.focus = function() {
<a name="line352"></a>  goog.base(this, &#39;focus&#39;);
<a name="line353"></a>
<a name="line354"></a>  if (goog.userAgent.OPERA) {
<a name="line355"></a>    // select() doesn&#39;t focus &lt;input&gt; elements in Opera.
<a name="line356"></a>    this.userInputEl_.focus();
<a name="line357"></a>  }
<a name="line358"></a>  this.userInputEl_.select();
<a name="line359"></a>};
<a name="line360"></a>
<a name="line361"></a>
<a name="line362"></a>/**
<a name="line363"></a> * Sets the default value of the prompt when it is displayed.
<a name="line364"></a> * @param {string} defaultValue The default value to display.
<a name="line365"></a> */
<a name="line366"></a>goog.ui.Prompt.prototype.setDefaultValue = function(defaultValue) {
<a name="line367"></a>  this.defaultValue_ = defaultValue;
<a name="line368"></a>};
<a name="line369"></a>
<a name="line370"></a>
<a name="line371"></a>/**
<a name="line372"></a> * Handles the closing of the prompt, invoking the callback function that was
<a name="line373"></a> * registered to handle the value returned by the prompt.
<a name="line374"></a> * @param {goog.ui.Dialog.Event} e The dialog&#39;s selection event.
<a name="line375"></a> * @private
<a name="line376"></a> */
<a name="line377"></a>goog.ui.Prompt.prototype.onPromptExit_ = function(e) {
<a name="line378"></a>  /*
<a name="line379"></a>   * The timeouts below are required for one edge case. If after the dialog
<a name="line380"></a>   * hides, suppose validation of the input fails which displays an alert. If
<a name="line381"></a>   * the user pressed the Enter key to dismiss the alert that was displayed it
<a name="line382"></a>   * can trigger the event handler a second time. This timeout ensures that the
<a name="line383"></a>   * alert is displayed only after the prompt is able to clean itself up.
<a name="line384"></a>   */
<a name="line385"></a>  if (!this.isClosing_) {
<a name="line386"></a>    this.isClosing_ = true;
<a name="line387"></a>    if (e.key == &#39;ok&#39;) {
<a name="line388"></a>      goog.Timer.callOnce(
<a name="line389"></a>          goog.bind(this.callback_, this, this.userInputEl_.value), 1);
<a name="line390"></a>    } else {
<a name="line391"></a>      goog.Timer.callOnce(goog.bind(this.callback_, this, null), 1);
<a name="line392"></a>    }
<a name="line393"></a>  }
<a name="line394"></a>};
<a name="line395"></a>
<a name="line396"></a>
<a name="line397"></a>/** @override */
<a name="line398"></a>goog.ui.Prompt.prototype.disposeInternal = function() {
<a name="line399"></a>  goog.dom.removeNode(this.userInputEl_);
<a name="line400"></a>
<a name="line401"></a>  goog.events.unlisten(this, goog.ui.Dialog.EventType.SELECT,
<a name="line402"></a>      this.onPromptExit_, true, this);
<a name="line403"></a>
<a name="line404"></a>  goog.ui.Prompt.superClass_.disposeInternal.call(this);
<a name="line405"></a>
<a name="line406"></a>  this.defaulValue_ = null;
<a name="line407"></a>  this.userInputEl_ = null;
<a name="line408"></a>};
</pre>


</body>
</html>
